<template>
  <div>
    <!-- title；弹窗的标题 -->
    <!-- visible：控制弹窗是否可见 -->
    <!-- before-close：弹窗关闭前的回调函数 -->
    <el-dialog
      :title="info.type === 'add' ? '新增菜单' : '编辑菜单'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="ruleForm.type">
            <el-radio :label="1">目录</el-radio>
            <el-radio :label="2">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="ruleForm.type == 2" label="上级" prop="pid">
          <el-select v-model="ruleForm.pid">
            <el-option label="顶级目录" :value="0"></el-option>
            <el-option
              v-for="(item, index) in optionList"
              :key="index"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type == 1" label="图标" prop="icon">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type == 2" label="地址" prop="url">
          <el-input v-model="ruleForm.url"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button v-if="info.type === 'add'" type="primary" @click="submitForm">立即创建</el-button>
          <el-button v-else type="primary" @click="submitForm">立即编辑</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getMenuList, editMenu } from "../../api";
export default {
  props: ["info"],
  data() {
    return {
      // 下拉框的数据
      optionList: [],
      // 表单中的数据
      ruleForm: {
        pid: 0,
        title: "",
        icon: "",
        // 1目录2菜单
        type: 1,
        url: "",
        // 1正常2禁用
        status: 1,
      },
      // 校验规则
      rules: {
        title: [{ required: true, message: "请输入菜单标题", trigger: "blur" }],
      },
    };
  },
  created() {
    // 获取下拉框中的数据
    this.getOptionList();
  },
  methods: {
    // 设置表单的值
    setVal(data) {
      // 深拷贝，避免数据互相影响
      this.ruleForm = { ...data };
    },
    async getOptionList() {
      let res = await getMenuList();
      console.log(res);
      this.optionList = res.list;
    },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if(this.info.type === 'add') {
            // 添加
            this.$http
              .post("/menuadd", this.ruleForm)
              .then((res) => {
                console.log(res);
              })
              .catch((err) => {
                console.log(err);
              });
            // 关闭弹窗
            this.close();
          }else {
            // 编辑
            let res = await editMenu(this.ruleForm);
            // 关闭弹窗
            this.close();
          }
          // 更新list
          this.$emit('update')
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 关闭弹窗
    close() {
      this.info.isShow = false;
    },
  },
};
</script>

<style>
</style>